package com.chuangke.report.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.common.report.JasperReportData;
import com.chuangke.common.report.JasperReportUtils;
import com.chuangke.report.model.Materail;

import net.sf.jasperreports.engine.JasperPrint;

@RestController
@RequestMapping("/jasper")
public class JasperController {

	@GetMapping("/m1")
	public void materail1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JasperReportUtils.print(request, response, "material", getData());
	}

	@GetMapping("/m2")
	public void materail2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JasperReportUtils.export(request, response, "pdf", "material", "物资", getData());
	}
	
	@GetMapping("/m3")
	public void materail3(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<JasperPrint> list = new ArrayList<>() ;
		list.add(JasperReportUtils.getJasperPrint("material", getData())) ;
		
		JasperReportData data = new JasperReportData() ;
		data.putParam("user", "张三") ;
		data.putParam("age", "42") ;
		list.add(JasperReportUtils.getJasperPrint("Blank_A4",data )) ;
		
		JasperReportUtils.printBatch(request, response, list);
	}

	private JasperReportData getData() {
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("date", "2019-04-13");
		parameters.put("dept", "生产自造部");

		List<Materail> list = new ArrayList<>();
		for (int i = 0; i < 9; i++) {
			list.add(new Materail("编码code" + i, "物料" + i, i + ""));
		}

		JasperReportData data = new JasperReportData();
		data.setParameters(parameters);
		data.setDetails(list);

		return data;
	}

}
